
import utis.ListNode;
import utis.doubleNode;

public class NodeJava {
    public static void main(String[] args) {
        // ListNode headerNode = new ListNode(0);
        // headerNode = createNode(headerNode,10);
        // printNode(headerNode);
        var headerDoubleNode = new doubleNode<>("hello");
        System.out.println(headerDoubleNode.preNode==null);
        headerDoubleNode = createDoubleNode(headerDoubleNode, 10,"world");
        printNode(headerDoubleNode);
        System.out.println("修改节点值");
        var modifyNode = searchDN(headerDoubleNode, 10);
        modifyNode.value = "modified";
        printNode(headerDoubleNode);
        System.out.println("插入节点");
        var newNode = new doubleNode<>("123");
        var newNode1 = new doubleNode<>("123");
        insert(modifyNode, newNode);
        insert(modifyNode, newNode1);
        printNode(headerDoubleNode);
        printReverseNode(headerDoubleNode);
        System.out.println("删除节点");
        delete(modifyNode);
        printNode(headerDoubleNode);
        printReverseNode(headerDoubleNode);

        
    }

    public static ListNode createNode(ListNode headerNode,int enlarge){
        ListNode firstNode=headerNode;
        for (int i = 0; i < enlarge;i++){
            ListNode node = new ListNode(i);
            headerNode.next = node;
            // headerNode.next = node.preNode;
            headerNode=node;

        }
        return firstNode;
        
    }
    public static void printNode(ListNode headerNode){
        var printStr = "";
        while (headerNode.next != null) {
            printStr = printStr + headerNode.toString();
            headerNode=headerNode.next;
        }
        System.out.println(printStr);
    }

    /**
     * 
     * @param doubleNode<T>
     * @param headerNode
     * @apiNote 泛型，双链表，遍历打印
     */
    public static <T> void printNode(doubleNode<T> headerNode){
        var printStr = "";
        int nums=0;
        while (headerNode != null) {
            printStr = printStr + headerNode.toString();
            headerNode=headerNode.next;
            nums++;
        }
        System.out.println(printStr+"个数："+nums);
    }
    public static <T> doubleNode<T> createDoubleNode(doubleNode<T> node,int nodeNums,T value) {
        doubleNode<T> headerNode = node;
        for(int i=0; i< nodeNums;i++){
            var newNode = new doubleNode<T>();
            newNode.value = value;
            node.next = newNode;
            newNode.preNode = node;
            node=newNode;
        }
        return headerNode;
    }
    public static <T> doubleNode<T> searchDN(doubleNode<T> node,int index){
        int i =0;
        while(node != null && i<index){
            node=node.next;
            i++;
        }

        return node;
    }
    public static <T> void insert(doubleNode<T> nodeAfter, doubleNode<T> newNode){
        if (nodeAfter.next != null) {
            newNode.next = nodeAfter.next;
            nodeAfter.next.preNode = newNode;
            nodeAfter.next=newNode;
            newNode.preNode=nodeAfter;
        }else{
            nodeAfter.next = newNode;
        }

    }
    public static <T> void delete(doubleNode<T> deleteNode){
        if (deleteNode.next == null) {
        
            deleteNode.preNode.next=null;
        }
        deleteNode.preNode.next=deleteNode.next;
        deleteNode.next.preNode=deleteNode.preNode;
        deleteNode.next=null;
        deleteNode.preNode=null;
        
    }
    public static <T> void printReverseNode(doubleNode<T> headerNode){
        var printStr = "";
        int nums=0;
        while (headerNode.next != null) {
            headerNode=headerNode.next;
        }
        while (headerNode != null) {
            printStr = printStr + headerNode.toString();
            headerNode=headerNode.preNode;
            nums++;
        }
        System.out.println(printStr+"个数："+nums);
    }

}
